package com.njsoft.bodyawakening.photo;

import android.app.Activity;


import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.njsoft.bodyawakening.utils.ActivityUtils;
import com.njsoft.bodyawakening.utils.SPUtileKey;
import com.njsoft.bodyawakening.view.MyToast;

import java.util.List;


/**
 * author: Cuiernao
 * date:   On 2018/10/18
 */
public class PhotoUtils {

    public static void setCAMERAPermission(final Activity context, final String selectType) {

        XXPermissions.with(context)
                //.constantRequest() //可设置被拒绝后继续申请，直到用户授权或者永久拒绝
                //.permission(Permission.SYSTEM_ALERT_WINDOW, Permission.REQUEST_INSTALL_PACKAGES) //支持请求6.0悬浮窗权限8.0请求安装权限
                .permission(Permission.WRITE_EXTERNAL_STORAGE, Permission.CAMERA) //不指定权限则自动获取清单中的危险权限
                .request(new OnPermission() {
                    @Override
                    public void hasPermission(List<String> granted, boolean isAll) {
                        if (isAll) {
                            //TODO 传递标记
                            if (selectType.equals("Photos")) {
                                ActivityUtils.goToActivityWithFlag(context, GetPhotoActivity.class,
                                        SPUtileKey.PHOTO_FLAG, SPUtileKey.PHOTO_XiangCe);
                            } else {
                                ActivityUtils.goToActivityWithFlag(context, GetPhotoActivity.class,
                                        SPUtileKey.PHOTO_FLAG, SPUtileKey.PHOTO_PaiZhao);
                            }
                        }else {
                            MyToast.show(context, "The access permission was successful, and some rights were not granted.");

                        }
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {
                        if(quick) {
                            MyToast.show(context, "Permanently denied authorization, please grant permission manually");

                            //如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.gotoPermissionSettings(context);
                        }else {
                            MyToast.show(context, "No, I can't take pictures.");

                        }
                    }
                });
    }
}
